package com.neutech.dao.impl;

import com.neutech.constant.Constant;
import com.neutech.dao.CartDAO;
import com.neutech.entity.Business;
import com.neutech.entity.Cart;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CartDAOImpl implements CartDAO {
    @Override
    public Cart getByFoodIdAndUserId(Integer foodId, Integer userId) {

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Cart cart = null;

        try {
            connection = Constant.DATASOURCE.getConnection();
            String sql = "select * from e_cart where food_id = ? and user_id = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, foodId);
            preparedStatement.setInt(2, userId);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                cart = new Cart();
                cart.setId(resultSet.getInt("id"));
                cart.setFoodId(resultSet.getInt("food_id"));
                cart.setFoodName(resultSet.getString("food_name"));
                cart.setFoodMainImage(resultSet.getString("food_main_image"));
                cart.setFoodPrice(resultSet.getBigDecimal("food_price"));
                cart.setBusinessId(resultSet.getInt("business_id"));
                cart.setUserId(resultSet.getInt("user_id"));
                cart.setQuantity(resultSet.getInt("quantity"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return cart;
    }
    @Override
    public int add(Cart cart) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int row = 0;
        try {
            connection = Constant.DATASOURCE.getConnection();
            String sql = "insert into e_cart (food_id, food_name, food_main_image, food_price," +
                    " business_id, user_id, quantity) values(?,?,?,?,?,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, cart.getFoodId());
            preparedStatement.setString(2, cart.getFoodName());
            preparedStatement.setString(3, cart.getFoodMainImage());
            preparedStatement.setBigDecimal(4, cart.getFoodPrice());
            preparedStatement.setInt(5, cart.getBusinessId());
            preparedStatement.setInt(6, cart.getUserId());
            preparedStatement.setInt(7, cart.getQuantity());
            row = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return row;
    }


    @Override
    public int updateQuantity(Cart cart) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int row = 0;
        try {
            connection = Constant.DATASOURCE.getConnection();
            String sql = "update e_cart set quantity = ? where id = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, cart.getQuantity());
            preparedStatement.setInt(2, cart.getId());
            row = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return row;
    }

    @Override
    public List<Cart> listByUserId(Integer userId) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<Cart> cartList = new ArrayList<>();

        try {
            connection = Constant.DATASOURCE.getConnection();
            String sql = "select * from e_cart where user_id = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, userId);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Cart cart = new Cart();
                cart.setId(resultSet.getInt("id"));
                cart.setFoodId(resultSet.getInt("food_id"));
                cart.setFoodName(resultSet.getString("food_name"));
                cart.setFoodMainImage(resultSet.getString("food_main_image"));
                cart.setFoodPrice(resultSet.getBigDecimal("food_price"));
                cart.setBusinessId(resultSet.getInt("business_id"));
                cart.setUserId(resultSet.getInt("user_id"));
                cart.setQuantity(resultSet.getInt("quantity"));

                cartList.add(cart);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return cartList;

    }

    @Override
    public Cart getByIdAndUserId(Integer id, Integer userId) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Cart cart = null;

        try {
            connection = Constant.DATASOURCE.getConnection();
            String sql = "select * from e_cart where id = ? and user_id = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            preparedStatement.setInt(2, userId);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                cart = new Cart();
                cart.setId(resultSet.getInt("id"));
                cart.setFoodId(resultSet.getInt("food_id"));
                cart.setFoodName(resultSet.getString("food_name"));
                cart.setFoodMainImage(resultSet.getString("food_main_image"));
                cart.setFoodPrice(resultSet.getBigDecimal("food_price"));
                cart.setBusinessId(resultSet.getInt("business_id"));
                cart.setUserId(resultSet.getInt("user_id"));
                cart.setQuantity(resultSet.getInt("quantity"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return cart;
    }

    @Override
    public int updateQuantityById(Integer id, Integer quantity) {
        Cart cart = new Cart();
        cart.setId(id);
        cart.setQuantity(quantity);
        return updateQuantity(cart);
    }


}
